package com.beiing.tianshuai.tianshuai.dongtai.ui;

import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.base.BaseActivity;
import com.beiing.tianshuai.tianshuai.entity.ConsumeMarksBean;
import com.beiing.tianshuai.tianshuai.entity.UserInfoBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.DensityUtils;
import com.tianshuai.easypopup.EasyPopup;
import com.tianshuai.easypopup.HorizontalGravity;
import com.tianshuai.easypopup.VerticalGravity;

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

public class DynamicMessageActivity extends BaseActivity {

    @BindView(R.id.ib_back)
    ImageButton mIbBack;
    @BindView(R.id.toolbar_filter)
    ImageButton mToolbarFilter;
    @BindView(R.id.toolbar_center_title)
    TextView mToolbarTitle;
    @BindView(R.id.fl_fragment_container)
    FrameLayout mVpFragmentContainer;

    /**
     * 更多菜单弹窗
     */
    private EasyPopup mPopup;

    private DynamicAllMessageFragment a;
    private DynamicPraiseMessageFragment b;
    private DynamicReplyMessageFragment c;

    private TextView mActionAll;
    private TextView mActionComment;
    private TextView mActionPraise;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_dynamic_message;
    }

    @Override
    protected void init() {
        String uid = UserInfoBean.getUid(mContext);

        mPopup = new EasyPopup(mContext)
                .setContentView(R.layout.menu_dynamic_message_filter)
                .setAnimationStyle(R.style.QQPopAnim)
                .setFocusAndOutsideEnable(true)
                .createPopup();
        initPopupView();
        initPopupWindow();

        initListener();
        consumePraiseMarks(uid);
        consumeReplyMarks(uid);

        a = new DynamicAllMessageFragment();
        b = new DynamicPraiseMessageFragment();
        c = new DynamicReplyMessageFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.fl_fragment_container, a);
        transaction.add(R.id.fl_fragment_container, c);
        transaction.add(R.id.fl_fragment_container, b);
        transaction.hide(b).hide(c).show(a).commit();

    }

    private void initPopupView() {
        mActionAll = mPopup.getView(R.id.action_all);
        mActionComment = mPopup.getView(R.id.action_comment);
        mActionPraise = mPopup.getView(R.id.action_praise);
        mActionAll.setEnabled(false);
        mActionAll.setTextColor(ContextCompat.getColor(mContext, R.color.blue_67a0ff));
    }

    private void initPopupWindow() {
        mActionAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mActionAll.setEnabled(false);
                mActionComment.setEnabled(true);
                mActionPraise.setEnabled(true);
                mActionAll.setTextColor(ContextCompat.getColor(mContext, R.color.blue_67a0ff));
                mActionComment.setTextColor(ContextCompat.getColor(mContext, R.color.textColorDark));
                mActionPraise.setTextColor(ContextCompat.getColor(mContext, R.color.textColorDark));
                mToolbarTitle.setText("动态通知");
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.hide(b).hide(c).show(a).commit();
                mPopup.dismiss();
            }
        });

        mActionComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mActionAll.setEnabled(true);
                mActionComment.setEnabled(false);
                mActionPraise.setEnabled(true);
                mActionAll.setTextColor(ContextCompat.getColor(mContext, R.color.textColorDark));
                mActionComment.setTextColor(ContextCompat.getColor(mContext, R.color.blue_67a0ff));
                mActionPraise.setTextColor(ContextCompat.getColor(mContext, R.color.textColorDark));
                mToolbarTitle.setText("评论");
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.hide(a).hide(b).show(c).commit();
                mPopup.dismiss();
            }
        });

        mActionPraise.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mActionAll.setEnabled(true);
                mActionComment.setEnabled(true);
                mActionPraise.setEnabled(false);
                mActionAll.setTextColor(ContextCompat.getColor(mContext, R.color.textColorDark));
                mActionComment.setTextColor(ContextCompat.getColor(mContext, R.color.textColorDark));
                mActionPraise.setTextColor(ContextCompat.getColor(mContext, R.color.blue_67a0ff));
                mToolbarTitle.setText("点赞");
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.hide(a).hide(c).show(b).commit();
                mPopup.dismiss();
            }
        });
    }

    private void initListener() {
        // 返回
        mIbBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        // 筛选
        mToolbarFilter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int i = view.getLayoutParams().width / 2 - DensityUtils.dp2px(mContext, 2);
                mPopup.showAtAnchorView(view, VerticalGravity.BELOW, HorizontalGravity.ALIGN_RIGHT, i, -DensityUtils.dp2px(mContext, 15));
            }
        });
    }

    public void consumeReplyMarks(String uid) {
        DisposableObserver<ConsumeMarksBean> observer = new DisposableObserver<ConsumeMarksBean>() {
            @Override
            public void onNext(@NonNull ConsumeMarksBean consumeMarksBean) {

            }

            @Override
            public void onError(@NonNull Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        };

        DataManager.getConsumeCommentMarksResult(uid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    public void consumePraiseMarks(String uid) {
        DisposableObserver<ConsumeMarksBean> observer = new DisposableObserver<ConsumeMarksBean>() {
            @Override
            public void onNext(@NonNull ConsumeMarksBean consumeMarksBean) {

            }

            @Override
            public void onError(@NonNull Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        };

        DataManager.getConsumeThingMarksResult(uid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
}
